//
//  MainViewController.swift
//  微博
//
//  Created by Lark on 16/2/22.
//  Copyright © 2016年 Lark. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildViewControllers()
        
        //设置按钮背景色
//        tabBar.tintColor = UIColor.orangeColor()
        
        setupTabbar()
    }
    
    override func viewWillAppear(animated: Bool) {
        
        super.viewWillAppear(animated)
        //将按钮设置到顶部
        tabBar.bringSubviewToFront(composeButton)
        
        //计算按钮宽度
        //-1 ：防止边界处理
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count) - 1
        composeButton.frame = CGRectInset(tabBar.bounds, width * 2, 0)
    }
    
    // MARK: - 监听方法
    //@objc 关键字 能够在编译的时候 让函数能够使用oc的消息机制 被调用到
    @objc private func clickComposeButton() {
        print("点击撰写按钮")
    }

    // MARK: - 私有控件
    ///撰写按钮
    private lazy var composeButton:UIButton = {
    
        let button = UIButton()
        
        let imageName = "tabbar_compose_icon_add"
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        
        let backImageName = "tabbar_compose_button"
        button.setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
        button.setBackgroundImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        
        //让按钮根据背景图片大小调整大小
        button.sizeToFit()
        
        return button
    
    }()
    
}

// MARK: - 设置tabBar
extension MainViewController{
    
    private func setupTabbar() {
        
        
        
        
        
        
        //设置tabbar背景图片 以下两句代码必须连用 Nav处理类似
        tabBar.shadowImage = UIImage.hm_singleDotImage(UIColor(white: 0.9, alpha: 1.0))
        tabBar.backgroundImage = UIImage(named: "tabbar_background")
        
        //添加撰写按钮
        tabBar.addSubview(composeButton)
        
        //添加监听方法
        composeButton.addTarget(self, action: "clickComposeButton", forControlEvents: .TouchUpInside)
        
    }
   
}

// MARK: - 添加控制器
extension MainViewController {
    
    ///添加所有子控制器
    private func addChildViewControllers() {
        
        addChildViewController(HomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageName: "tabbar_message_center")
        
        //添加一个空白的控制器
        addChildViewController(UIViewController())
        
        addChildViewController(DiscoverViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageName: "tabbar_profile")
        
        
    }
    /// 添加一个控制器
    ///
    /// - parameter vc:        视图控制器
    /// - parameter title:     标题
    /// - parameter imageName: 图像名称
    private func addChildViewController(vc : UIViewController,title : String,imageName : String) {
  
        vc.title = title
        
        //设置文本属性
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
        
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)//渲染模式
        
        let nav = UINavigationController(rootViewController: vc)
        
        //添加子控制器
        addChildViewController(nav)
        
    }
    
}